﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;

namespace ShopNet.Model
{
    public static class NServico
    {
        #region ShopNetDataContext

        private static ShopNetDataContext db;
        public static ShopNetDataContext Db
        {
            get { return db ?? (db = new ShopNetDataContext()); }
        }

        #endregion
        
        #region Pagina

        public static IQueryable<T> Pagina<T, TResult>(this IQueryable<T> obj, int page, int pageSize, Expression<Func<T, TResult>> orderSelector, bool asc)//, out int rowsCount)
        {
            //rowsCount = obj.Count();
            int innerRows = (page - 1) * pageSize;
            if (asc)
            {
                if (orderSelector != null) return obj.OrderBy(orderSelector).Skip(innerRows).Take(pageSize).AsQueryable();
            }
            else
            {
                if (orderSelector != null)
                    return obj.OrderByDescending(orderSelector).Skip(innerRows).Take(pageSize).AsQueryable();
            }

            return obj.Skip(innerRows).Take(pageSize).AsQueryable();
        }

        #endregion

    }
}